using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using HSHLogicLayer.AuthWS;
using HSHLogicLayer;

namespace TaskVision
{
	public partial class ManageUsersForm : System.Windows.Forms.Form
	{
		#region private Classes

		//this is the object we use for the ListBox
		private class UserFullNameAndID 
		{
			private int m_UserID;
			private string m_UserFullName;

			public UserFullNameAndID(int uID, string uFullName) 
			{
				m_UserID = uID;
				m_UserFullName = uFullName;
			}

			public int UserID  
			{
				get {return m_UserID;}
				set {m_UserID = value;}
			}

			public string UserFullName  
			{
				get {return m_UserFullName;}
				set {m_UserFullName = value;}
			}
		}
		#endregion

		public ManageUsersForm()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();

		}

		private void ManageUsersForm_Load(object sender, System.EventArgs e)
		{
			PopulateListBox();
		}

		private void PopulateListBox() 
		{
			if (DataLayerSingleton.Instance().DsLookupTables != null) 
			{
				lbUsers.Items.Clear();
				lbUsers.DisplayMember = "UserFullName";
                foreach (DataRow dr in DataLayerSingleton.Instance().DsLookupTables.Users.Rows)
					lbUsers.Items.Add(new UserFullNameAndID((int) dr["UserID"], (string) dr["UserFullName"]));
			}
		}

		private void lbUsers_SelectedIndexChanged(object sender, System.EventArgs e)
		{
            if (lbUsers.SelectedItem != null)
            {
                btnEdit.Enabled = true;
                btnAccunts.Enabled = true;
            }
		}

		private void lbUsers_DoubleClick(object sender, System.EventArgs e)
		{
			DisplayEditForm();
		}

		private void DisplayEditForm() 
		{
			if (lbUsers.SelectedItem != null) 
			{
                DataRow userRow = DataLayerSingleton.Instance().DsLookupTables.Users.Rows.Find(((UserFullNameAndID)lbUsers.SelectedItem).UserID);
				EditUserForm eForm = new EditUserForm(userRow);
				DialogResult eFormResult = eForm.ShowDialog();
		
				if (eFormResult == DialogResult.OK) 
					PopulateListBox();
			}
		}

		private void btnAdd_Click(object sender, System.EventArgs e)
		{
			AddUserForm aForm = new AddUserForm();
			DialogResult aFormResult = aForm.ShowDialog();

			if (aFormResult == DialogResult.OK) 
				PopulateListBox();
		}

		private void btnEdit_Click(object sender, System.EventArgs e)
		{
			DisplayEditForm();
		}

		private void btnClose_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

        private void btnAccunts_Click(object sender, EventArgs e)
        {
            if (lbUsers.SelectedItem != null)
            {
                this.Cursor = Cursors.WaitCursor;
                ManageUserAccunts mAccunts = new ManageUserAccunts(((UserFullNameAndID)lbUsers.SelectedItem).UserID, ((UserFullNameAndID)lbUsers.SelectedItem).UserFullName);
                mAccunts.ShowDialog();
                this.Cursor = Cursors.Default;
            }
        }

		
	}
}
